package com.ruoyi.web.controller.easywenku;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.easywenku.entity.StudentDetails;
import com.ruoyi.easywenku.service.StudentDetailsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生详情Controller
 *
 * @author yx
 * @date 2021-08-16
 */
@Controller
@RequestMapping("/easywenku/studentDetails")
public class StudentDetailsController extends BaseController
{
    private String prefix = "easywenku/studentDetails";

    @Autowired
    private StudentDetailsService StudentDetailsService;

    @RequiresPermissions("easywenku:studentDetails:view")
    @GetMapping()
    public String studentDetails( ModelMap mmap)
    {

        List<StudentDetails> list = StudentDetailsService.lambdaQuery().select(StudentDetails::getBatch)
                .isNotNull(StudentDetails::getBatch)
                .groupBy(StudentDetails::getBatch).list();

        mmap.put("batchList",list );

        return prefix + "/studentDetails";
    }

    /**
     * 查询学生详情列表
     * 查询学生详情列表
     */
    @RequiresPermissions("easywenku:studentDetails:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StudentDetails studentDetails)
    {
        startPage();
        List<StudentDetails> list = StudentDetailsService.lambdaQuery()
                .eq(StrUtil.isNotBlank(studentDetails.getBatch()),StudentDetails::getBatch, studentDetails.getBatch())
                .eq(StrUtil.isNotBlank(studentDetails.getCompleted()),StudentDetails::getCompleted, studentDetails.getCompleted())
                .like(StudentDetails::getName,studentDetails.getName())
                .like(StudentDetails::getIdentificationNumber,studentDetails.getIdentificationNumber())
                .like(StudentDetails::getMobilePhone,studentDetails.getMobilePhone())
                .orderByDesc(StudentDetails::getId).list();
        return getDataTable(list);
    }

    /**
     * 导出学生详情列表
     */
    @RequiresPermissions("easywenku:studentDetails:export")
    @Log(title = "学生详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StudentDetails StudentDetails)
    {
        List<StudentDetails> list = StudentDetailsService.list(new QueryWrapper<>(StudentDetails).orderByDesc("id"));
        ExcelUtil<StudentDetails> util = new ExcelUtil<StudentDetails>(StudentDetails.class);
        return util.exportExcel(list, "studentDetails");
    }

    /**
     * 新增学生详情
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存学生详情
     */
    @RequiresPermissions("easywenku:studentDetails:add")
    @Log(title = "学生详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StudentDetails StudentDetails)
    {
        return toAjax(StudentDetailsService.save(StudentDetails));
    }

    /**
     * 修改学生详情
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        StudentDetails studentDetails = StudentDetailsService.getById(id);
        mmap.put("studentDetails", studentDetails);
        return prefix + "/edit";
    }

    /**
     * 修改保存学生详情
     */
    @RequiresPermissions("easywenku:studentDetails:edit")
    @Log(title = "学生详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StudentDetails StudentDetails)
    {
        return toAjax(StudentDetailsService.updateById(StudentDetails));
    }

    /**
     * 删除学生详情
     */
    @RequiresPermissions("easywenku:studentDetails:remove")
    @Log(title = "学生详情", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(StudentDetailsService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
